layui.use(['laypage', 'element', 'form'], function () {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;


    //监听选择楼宇,填充楼层
    form.on('select(building)', function (data) {
        if (data.value=="" || data.value==null ||data.value==undefined){
            return;
        }
        $("#buildingId").val(data.value);
        $.ajax({
            url: "/gbElderly/selectFloors?buildingId=" + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs.code == 200) {
                    var floors = rs.data;
                    $("#floors").empty().append('<option value="">请选择楼层</option>');
                    for (var i = 1; i <= floors; i++) {
                        $("#floors").append('<option value="' + i + '">' + i + '层</option>');
                    }
                    if (floors <= 0) {
                        layer.msg("该楼宇未配置楼层，请完成配置后操作")
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });

    //监听选择楼宇层数，填充房间
    form.on('select(floors)', function (data) {
        if (data.value=="" || data.value==null ||data.value==undefined){
            return;
        }
        $("#floor").val(data.value);
        var buildingId = $("#buildingId").val();
        $.ajax({
            url: "/gbElderly/selectRooms?buildingId=" + buildingId + '&floor=' + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs.code == 200) {
                    $("#rooms").empty().append('<option value="">请选择房间</option>');
                    var rooms = rs.data;
                    for (var i = 0; i < rooms.length; i++) {
                        $("#rooms").append('<option value="' + rooms[i].id + '">' + rooms[i].code + '</option>');
                    }
                    if (rooms.length <= 0 || rooms == null || rooms == undefined) {
                        layer.msg("该楼层未配置房间，请完成配置后操作")
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });

    //监听选择房间，填充床位
    form.on('select(rooms)', function (data) {
        if (data.value=="" || data.value==null ||data.value==undefined){
            return;
        }
        var buildingId = $("#buildingId").val();
        var floor = $("#floor").val();
        $("#room").val(data.value);
        $.ajax({
            url: "/gbElderly/selectBeds?buildingId=" + buildingId + '&floor=' + floor + '&roomId=' + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs.code == 200) {
                    $("#beds").empty().append('<option value="">请选择空闲床位</option>');
                    var beds = rs.data;
                    for (var i = 0; i < beds.length; i++) {
                        $("#beds").append('<option value="' + beds[i].id + '">' + beds[i].code + '</option>');
                    }
                    if (beds.length <= 0 || beds == null || beds == undefined) {
                        layer.msg("该房间未配置床位或已无空闲床位")
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });

    //监听选择床位，填充床位类型
    form.on('select(beds)', function (data) {
        if (data.value=="" || data.value==null ||data.value==undefined){
            return;
        }
        $.ajax({
            url: "/gbElderly/selectBedType?id=" + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs.code == 200) {
                    var type = rs.data.type;
                    $("#bedId").val(rs.data.id);
                    $("#bedNo").val(rs.data.code);
                    if(type==0){
                        $("#typeName").val(rs.data.typeName);
                        $("#type").val(0)
                    }
                    if (type == 1) {
                        $("#typeName").val("普通床位")
                        $("#type").val(1)
                    }
                    if (type == 2) {
                        $("#typeName").val("护理床位")
                        $("#type").val(2)
                    }

                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });



    form.on('submit(submitBtn)', function(data){
        $.ajax({
            url: '/checkinrecords/update',
            data:data.field,
            type: 'post',
            dataType: 'json',
            async: false,
            success: function (rs) {
                if (rs.code === 200) {
                    parent.layer.close(parent.ee);
                    parent.layer.msg(rs.message);
                    parent.location.reload();
                } else {
                    layer.msg(rs.message);
                }
            }
        });

        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
});